<?php

/**
 * Enter description here...
 *
 * @author Administrator
 * @package defaultPackage
 * @rcsfile 	$RCSfile: search.php,v $
 * @revision 	$Revision: 1.1 $
 * @date 	$Date: 2009/08/04 04:06:21 $
 */
define('DEDE_FILE','search');

require_once dirname(__FILE__).'/model/common.inc.php';
require_once DEDEINC.'/datalistcp.class.php';
$q = addslashes(ereg_replace("[\"\r\n\t\*\?\(\)\$%'><]"," ",stripslashes(trim($q))));

if($q=='' || strlen($q) < 3 || strlen($q) > 30)
{
	ShowMsg("关键字长度必须要3-30字节之间！","-1");
	exit();
}
$status = (int)$status;
$qharray = array(0,1,2,3);
$h = intval($qharray[$h]);

$dtp = new DedeTemplate();
switch($h)
{
	case 0:
		$multistr = '';
		switch($status)
		{
				//未解决问题
			case 2:
				$multistr ='q='.($q).'&amp;h='.$h.'&amp;status=2';
				$statussql = 'a.status = 0 and';
				break;
				//精彩推荐
			case 3:
				$multistr ='q='.($q).'&amp;h='.$h.'&amp;status=3';
				$statussql = 'a.digest=1 and';
				break;
			//全部问题
			case 1:
			default:
				$multistr ='q='.($q).'&amp;h='.$h.'&amp;status=1';
				$statussql = ' a.status>=0 and';
				break;

		}
		$row = $dsql->getone("select count(*) as dd from `#@__ask` as a where $statussql a.title like '%$q%'");
		$askcount = $row['dd'];
		$realpages = @ceil($askcount/$tpp);
		if($page > $realpages)
		{
			$page = $realpages;
		}
		$page = isset($page) ? max(1, intval($page)) : 1;
		$start_limit = ($page - 1) * $tpp;

		$multipage = multiy($askcount,$realpages, $tpp, $page, "search.php?$multistr");
		$query = "select a.id, a.tid, a.tidname, a.tid2, a.tid2name, a.uid, a.title, a.reward, a.dateline, a.status, a.replies, a.content,m.userid from #@__ask as a left join  #@__member as m on m.mid=a.uid where $statussql a.title like '%$q%' limit $start_limit, $tpp";
		$dsql->Execute('me',$query);
		while($row = $dsql->getarray())
		{
			$row['fulltitle'] = $row['title'];
			$row['title'] = cn_substr($row['title'],60);
			$row['title'] = str_replace($q,'<b style="color:red;">'.$q.'</b>',$row['title']);
			$row['content'] = cn_substr($row['content'] ,500);
			$row['dateline'] = date('Y-m-d',$row['dateline']);
			$asks[] = $row;
		}

		break;
	case 1:
		$row = $dsql->getone("select count(*) as dd from `#@__doctor` where name like '%$q%'");
		$askcount = $row['dd'];
		$realpages = @ceil($askcount/$tpp);
		if($page > $realpages)
		{
			$page = $realpages;
		}
		$page = isset($page) ? max(1, intval($page)) : 1;
		$start_limit = ($page - 1) * $tpp;

		$multipage = multiy($askcount,$realpages, $tpp, $page, "search.php?");
		$query = "select * from `#@__doctor` where name like '%$q%'";
		$dsql->Execute('me',$query);
		while($row = $dsql->getarray())
		{
			$row['name'] = cn_substr($row['name'],40);
			$row['name'] = str_replace($q,'<b style="color:red;">'.$q.'</b>',$row['name']);
			$row[description] = cn_substr(strip_tags($row[description],''),300);
			$doctors[] = $row;
		}
		break;
	case 2:
		$query = "select * from `#@__archives` where title like '%$q%'";
		$dsql->Execute('me',$query);
		while($row = $dsql->getarray())
		{
			$row['title'] = cn_substr($row['title'],40);
			$row['title'] = str_replace($q,'<b style="color:red;">'.$q.'</b>',$row['title']);
			$archives[] = $row;
		}
		//print_r($archives);
		break;
	case 3:
		$query = "select * from `#@__hospital` where name like '%$q%' or intro like '%$q%'";
		$dsql->Execute('me',$query);
		while($row = $dsql->getarray())
		{
			$row['name'] = cn_substr($row['name'],40);
			$row['intro'] = cn_substr($row['intro'],430);
			$row['name'] = str_replace($q,'<b style="color:red;">'.$q.'</b>',$row['name']);
			$row['intro'] = str_replace($q,'<b style="color:red;">'.$q.'</b>',$row['intro']);
			$hospitals[] = $row;
		}
		break;
}
$navtitle = $q;
$dtp->LoadTemplate(ASKROOT.'/template/default/search.htm');
if($cfg_ask_rewrite=='Y')
{
	$dtp->Display();
	myecho();
	exit();
}
else
{
	$dtp->Display();
}

?>